<%@ page isELIgnored="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<c:set var="_path" value="${pageContext.request.contextPath}"></c:set>
<script>
function initIdPath(){
	$.ajax({
		url : '${_path}/resources/initResourcesIdPath',
		dataType:'json',
		success:function(data){
			alert(data.mes);
		}
	});
}
</script>
<input type="button" value="重置IdPath" onclick="initIdPath()" />
<div id="resourceTable"></div>
<script>
var moneyType = ${json.aba};
jQuery(function($){
	$('#resourceTable').treegrid({
		title:'Resource',
		iconCls:'icon-edit',
		nowrap: false,
		rownumbers: true,
		animate:true,
		url:'resources/queryList',
		idField:'resouceid',
		treeField:'name',
		dnd:true,
		frozenColumns:[[
			{field:'name',title:'名称',width:200}
		]],
		columns:[[
			{field:'type',title:'类别',width:50},
			{field:'url',title:'URL',width:200},
			{field:'ordernum',title:'排序号',width:50},
			{field:'icon',title:'图标样式',width:100}
		]],
		/*
		onBeforeLoad:function(row,param){
			if (row){
				$(this).treegrid('options').url = 'treegrid_subdata.json';
			} else {
				$(this).treegrid('options').url = 'treegrid_data.json';
			}
		},
		*/
		onLoadSuccess:function(){
			$('#resourceTable').treegrid('collapseAll');
		},
		onContextMenu: function(e,row){
			e.preventDefault();
			var obj = {};
			var div = $('<div ></div>').appendTo($('body'));
			$(div).append('<div iconCls="icon-add" command="add" style="width:150px;">新增</div>');
			$(div).append('<div iconCls="icon-edit" command="edit" style="width:150px;">修改</div>');
			$(div).append('<div iconCls="icon-remove" command="del" style="width:150px;">删除</div>');
			$(div).menu({
				onClick : function(item){
					if($(item.target).attr('command') == 'add'){
						addrow(row);
					}else if($(item.target).attr('command') == 'edit'){
						updaterow(row);
					}else if($(item.target).attr('command') == 'del'){
						deleterow(row);
					}
				}
			});
			$(div).menu('show', {
				left:e.pageX,
				top:e.pageY
			});
		}
	});
});
//新增
function addrow(r){
	showWindow({
			title:'增加资源',
			href:'${_path}/resources/addResourcesPopWindow?pid=' + r.resouceid,
			width:500,
			height:450
		});
}
function updaterow(rows){
	//这里有一个jquery easyui datagrid的一个小bug，必须把主键单独列出来，要不然不能多选
	if(rows.length==0){
		$.messager.alert('提示',"请选择你要更新的角色",'info');
		return;
	}
	if(rows.length > 1){
		$.messager.alert('提示',"只能选择一位角色进行更新",'info');
		return;
	}
	showWindow({
		title:'修改资源',
		href:'${_path}/resources/updateResourcesPopWindow?id=' + rows.resouceid,
		width:500,
		height:450
	});
}
//删除
function deleterow(row){
	$.messager.confirm('提示','该操作将删除资源及子资源,确定要删除吗?',function(result){
		if (result){
			$.post(
				'${_path}/resources/delete?id=' + row.resouceid,
				function(data){
					$('#resourceTable').treegrid('reload');  
					$.messager.alert('提示',data.mes,'info');
			});
		}
	});
}
//表格查询
function doSearch(){
	var params = $('#resourceTable').datagrid('options').queryParams;//先取得 datagrid的查询参数
	var fields =$('#queryForm').serializeArray();//自动序列化表单元素为JSON对象
	$.each( fields, function(i, field){
		params[field.name] = field.value;//设置查询参数
	}); 
	$('#resourceTable').datagrid('reload');
}
//清空查询条件
function clearForm(){
	$('#queryForm').form('clear');
	doSearch();
}
</script>